package com.jilujia.domain.user;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.jilujia.domain.user.repository.UserVoteQuestionRepository;
import com.jilujia.framework.domain.AbstractPoEntityManager;
import com.jilujia.framework.exception.BusinessException;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class UserVoteQuestionManager extends
		AbstractPoEntityManager<Long, UserVoteQuestion, UserVoteQuestionRepository> {

	/**
	 * 用户对answer投票，是否对user有用
	 * 
	 * @param userId
	 * @param answerId
	 * @param useful
	 */
	@Transactional
	public void vote(Long userId, Long questionId, boolean useful) {
		UserVoteQuestion userVote = this.getRepository().findByUserIdAndQuestionId(userId, questionId);
		if (userVote != null) {
			// TODO 应该为exception，否则其他方法不知道要不要catch
			throw new BusinessException("already vote");
		} else {
			userVote = new UserVoteQuestion(userId, questionId, useful);
			this.setInstance(userVote);
			this.save();
		}
	}
}
